package top.vanchor.flexible.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.log4j.Log4j2;
import top.vanchor.flexible.config.consts.SpecialType;
import top.vanchor.flexible.entity.Original;
import top.vanchor.flexible.entity.Special;
import top.vanchor.flexible.entity.dto.OriginalListDto;
import top.vanchor.flexible.mapper.OriginalMapper;
import top.vanchor.flexible.service.IOriginalService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import top.vanchor.flexible.utils.ImageAddress;
import top.vanchor.flexible.utils.JWTUtils;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Ding
 * @since 2024-10-09
 */
@Log4j2
@Service
public class OriginalServiceImpl extends ServiceImpl<OriginalMapper, Original> implements IOriginalService {

    @Override
    public IPage<Original> getList(OriginalListDto dto) {
        IPage<Original> ipage = baseMapper.getList(new Page<>(dto.getPageNo(), dto.getPageSize()), dto);
        ipage.getRecords().forEach(it->{
            it.setResourceUrl(ImageAddress.getAllPath(it.getResourceUrl()));
        });
        return ipage;
    }

    @Override
    public int doEdit(Original original) {
        if (original.getId() == null) {
            //新增
            original.setCreateTime(new Date());
            original.setUpdateTime(new Date());
            return baseMapper.insert(original);
        }
        //编辑
        original.setUpdateTime(new Date());
        return baseMapper.updateById(original);
    }

    @Override
    public int doDeleteBy(List<Integer> ids) {
        QueryWrapper<Original> wrapper = new QueryWrapper<>();
        wrapper.in("id", ids);
        int ret = baseMapper.delete(wrapper);
        if (ret > 0) {
            log.info("管理员{}（ID为{}）：删除了{}条数据，id为：{}", JWTUtils.getAuthorName(), JWTUtils.getAuthorId(), ids.size(), ids);
        }
        return ret;
    }
}
